package cn.levill.domain.strategy.model.entity;

import cn.levill.types.common.Constants;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author Levill
 * @description 策略规则实体
 * @date 2025/02/01 12:49
 */
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class StrategyRuleEntity {
    /**
     * 抽奖策略ID
     */
    private Long strategyId;
    /**
     * 抽奖奖品ID
     */
    private Integer awardId;
    /**
     * 抽奖规则类型【1-策略规则、2-奖品规则】
     */
    private Integer ruleType;
    /**
     * 抽奖规则类型【rule_lock】
     */
    private String ruleModel;
    /**
     * 抽奖规则比值
     */
    private String ruleValue;
    /**
     * 抽奖规则描述
     */
    private String ruleDesc;

    public Map<String, List<Integer>> getRuleWeightValues() {
        if (!"rule_weight".equals(ruleModel)) return null;
        String[] ruleValueGroups = ruleValue.split(Constants.SPACE);
        Map<String, List<Integer>> resultMap = new HashMap<>();
        for (String ruleValueGroup : ruleValueGroups) {
            // 检测输入是否为空
            if (ruleValueGroup == null || ruleValueGroup.isEmpty()) {
                return resultMap;
            }
            // 分割字符串以获得键和值
            String[] parts = ruleValueGroup.split(Constants.COLON);
            if (parts.length != 2) {
                throw new IllegalArgumentException("rule_weight rule_rule invalid input format" + ruleValueGroup);
            }
            List<Integer> values = Arrays.stream(parts[1].split(Constants.SPLIT))
                    .map(Integer::parseInt) // 转为Integer
                    .collect(Collectors.toList()); //收集为List
            resultMap.put(ruleValueGroup, values);
            // <"4000:102,103"=[102, 103]>
        }
        // 将键和值放入Map中
        return resultMap;
    }
}
